<template>
  <div class="app-container">
    <!-- *************************************搜索区************************************* -->
    <!-- 分类 -->
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" v-show="showSearch" ref="queryForm" size="small" :inline="true">
          <el-row>
            <el-form-item prop="type">
              <el-select @change="getList" clearable v-model="queryParams.type" :placeholder="$t('sku类型')">
                <el-option :label="$t('单品')" :value="1" />
                <el-option :label="$t('组合')" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item prop="isAgree">
              <el-select @change="getList" clearable v-model="queryParams.isAgree" :placeholder="$t('审批是否通过')">
                <el-option :label="$t('未审核')" :value="0" />
                <el-option :label="$t('通过')" :value="1" />
                <el-option :label="$t('驳回')" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item prop="skuNo">
              <el-input
                v-model="queryParams.skuNo"
                :placeholder="$t('sku编号')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="combinationName">
              <el-input
                v-model="queryParams.combinationName"
                :placeholder="$t('组合名称')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
    </PageSearch>

    <el-table
      @row-click="(row) => $rowClick(row, clickedRows)"
      :row-class-name="({ row }) => $tableRowClassName(row, clickedRows)"
      @selection-change="(rows) => (tableCheck = rows)"
      :height="calTableHeight()"
      v-loading="loading"
      ref="TableRef"
      :data="tableData"
      row-key="menuId"
    >
      <el-table-column align="center" show-overflow-tooltip prop="distrName" :label="$t('租户')" />
      <el-table-column align="center" show-overflow-tooltip prop="shopName" :label="$t('店铺名')" />
      <el-table-column align="center" :label="$t('图片')">
        <template slot-scope="{ row }">
          <!-- type	类型 1-单品sku 2-组合sku -->
          <el-image
            style="width: 50px; height: 50px"
            fit="contain"
            :key="Math.random()"
            v-hover="row.skuUrl"
            v-if="row.type == 1"
            :src="row.skuUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
          <el-image
            style="width: 50px; height: 50px"
            fit="contain"
            v-if="row.type == 2"
            :key="Math.random()"
            v-hover="row.combinationUrl"
            :src="row.combinationUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="type" :label="$t('类型')">
        <template slot-scope="{ row }">
          <!-- type	类型 1-单品sku 2-组合sku -->
          <div v-if="row.type == 1" :style="{ color: $randomColor(row.type) }">
            {{ $t('单品sku') }}
          </div>
          <div v-if="row.type == 2" :style="{ color: $randomColor(row.type) }">
            {{ $t('组合sku') }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('单品sku')">
        <template slot-scope="scope">
          <div @click="handleEdit(scope.row)" style="cursor: pointer; color: #1890ff">
            {{ scope.row.skuNo }}
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" show-overflow-tooltip prop="combinationName" :label="$t('组合名')" />

      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('审批是否通过')">
        <template #default="{ row }">
          <!-- 是否审批通过 0-未审核 1-通过 2-驳回 -->
          <div v-if="row.isAgree == 0" :style="{ color: $randomColor(row.isAgree) }">{{ $t('未审核') }}</div>
          <div v-if="row.isAgree == 1" :style="{ color: $randomColor(row.isAgree) }">{{ $t('通过') }}</div>
          <div v-if="row.isAgree == 2" :style="{ color: $randomColor(row.isAgree) }">{{ $t('驳回') }}</div>
        </template>
      </el-table-column>

      <el-table-column align="center" show-overflow-tooltip prop="createTime" :label="$t('申请时间')" />
      <el-table-column fixed="right" :label="$t('操作')" align="center" width="200">
        <template slot-scope="{ row }">
          <!-- 是否审批通过 0-未审核 1-通过 2-驳回 -->
          <el-button
            v-hasPermi="['distribution:skuSellerApplyProduce:edit']"
            type="text"
            @click="handlePass(row)"
            :disabled="row.isAgree != 0"
          >
            {{ $t('审核') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- sku详情/修改 -->
    <Skucontmoduler @relaod="relaod" ref="Skucontmoduler"></Skucontmoduler>
  </div>
</template>

<script>
import { skuSellerApplyProduceList } from '@/api/distribution/sku/productionApply'
import { updateSkuSellerApplyProduce } from '@/api/distribution/sku/productionApply'

import Skucontmoduler from '@/components/Skucontmoduler'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SellerSku',

  components: {
    Skucontmoduler,
  },
  data() {
    return {
      tableCheck: [],
      /**点击的表格项 */
      clickedRows: [],
      tableData: [],
      showSearch: false,
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        value2: 1,
      },
    }
  },
  watch: {
    'queryParams.pageNum': function (val) {
      this.clickedRows = []
    },
  },
  created() {
    this.getList()
  },
  methods: {
    handlePass(row) {
      this.$confirm(this.$t('确认通过?'), this.$t('提示'), {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '驳回',
      })
        .then(() => {
          const data = {
            // 是否审批通过 0-未审核 1-通过 2-驳回
            isAgree: 1,
            id: row.id,
          }
          updateSkuSellerApplyProduce(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
            }
          })
        })
        .catch((e) => {
          const data = {
            // 是否审批通过 0-未审核 1-通过 2-驳回
            isAgree: 2,
            id: row.id,
          }
          updateSkuSellerApplyProduce(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
            }
          })
        })
    },

    relaod() {
      this.getList()
    },
    handleEdit(row) {
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('basicInformationSeller')
    },
    // 取消申请
    cancel() {
      this.editDialogConfig = !this.editDialogConfig
    },

    /** 查询菜单列表 */
    getList() {
      this.loading = true
      skuSellerApplyProduceList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.value1 = null
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10

      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
.goods {
  &-img {
    &-box {
      position: relative;
    }

    &-mini {
      height: 50px;
      border-radius: 3px;

      &:hover {
        & + .goods-img-big {
          display: block;
        }
      }
    }

    &-big {
      // display: none;
      position: absolute;
      z-index: 100;
      top: -180px;
      left: 0;
      transform: translate(0, 70px);
      width: 200px;
      height: 200px;
      border-radius: 3px;
    }
  }
}

.bigImg {
  border-radius: 3px;
  z-index: 10;
  position: fixed;
  width: 200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
